Skip to content

tls_codec: impl for String#2351

Open
keks wants to merge 1 commit into
RustCrypto:masterfrom
cryspen:keks/tls_codec-impl-for-string
Open

tls_codec: impl for String#2351
keks wants to merge 1 commit into
RustCrypto:masterfrom
cryspen:keks/tls_codec-impl-for-string

Conversation

@keks

@keks keks commented Jun 18, 2026

Copy link
Copy Markdown

This implements tls_codec de/serialization for String and &str.

Fixes #2204

@franziskuskiefer franziskuskiefer self-requested a review June 18, 2026 13:09

@franziskuskiefer franziskuskiefer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.
But we need to pick a way to serialize here and describe it so that it's not used when it's not the right encoding.

Comment thread tls_codec/src/string.rs
@@ -0,0 +1,224 @@
use alloc::string::String;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a doc comment on top here that this is not specified in the TLS presentation language. But that it's what's implicitly done all the time. And then say how the string is actually being serialized (just its bytes).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do!

Comment thread tls_codec/src/string.rs
where
Self: Sized,
{
let bytes = TlsByteVecU8::tls_deserialize(bytes)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't right. You serialize to variable length encoding.
This goes back to the comment on top. We need to clearly describe how strings are being encoded if we want to provider a general implementation.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, I thought this would happen by itself. Alright. It looks like VLByteSlice doesn't implement De/SerializeBytes yet, and I would like to impl those as well for nostd support. Should I do that in this PR or a separate one?

Comment thread tls_codec/src/string.rs
#[cfg(feature = "std")]
#[test]
fn roundtrip_deserialize() {
let original = String::from("roundtrip test");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a longer string here would surface the incompatible serialize/deserialize. e.g. it will with let original = String::from_utf8(vec![0x30; 300]).unwrap();.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, that fails. I'll add it to ensure this is caught.

@keks

keks commented Jun 18, 2026

Copy link
Copy Markdown
Author

We discussed the next steps offline:

The new module needs more documentation, especially around what the format used here is. There are multiple possible encodings, but here we are focusing on varint-length encoded buffers containing utf8 bytes. I'll open a followup issue for supporting Strings with sequences of fixed-length integers in the length field.

Strings backed by arrays (i.e. fixed-length byte buffers) will not be supported. Restricting strings (esp with multibyte characters) to fixed byte lengths seems hard to work with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tls: Implement de/serialization for String

2 participants